home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9733 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.3 KB  |  183 lines

  1. Path: newsfeed.internetmci.com!panix!usenet
  2. From: traub@panix.com (Amy Weintraub)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: need help with linklists
  5. Date: 4 Mar 1996 03:47:29 GMT
  6. Organization: PANIX Public Access Internet and Unix, NYC
  7. Message-ID: <4hdp4h$sim@news1.panix.com>
  8. References: <4hd3qn$qo6@ixnews2.ix.netcom.com>
  9. NNTP-Posting-Host: traub.dialup.access.net
  10. Mime-Version: 1.0
  11. X-Newsreader: WinVN 0.99.2
  12.  
  13. In article <4hd3qn$qo6@ixnews2.ix.netcom.com>, wells2@ix.netcom.com says...
  14. >
  15. >
  16. >
  17. >does anyone have a simple code that does a linklist??  
  18.  
  19. >thanks
  20. >edward
  21. >
  22. > wells2@ix.netcom.com
  23. >
  24.  
  25. Edward,
  26.  
  27. I just wrote this simple program in C that uses a linked list. The program reads a file or 
  28. keyboard input from stdin and keeps track of how many times each word occurs then prints a 
  29. report one word per line saying how many times each word occurred.  Each word goes in a 
  30. node.  It probably isn't fully optimized, I'm only a second semester C student.  But, it 
  31. works, and it is simple.  I hope this helps.  
  32.  
  33. /* wclist.c -- reads a file or keyboard input from stdin and keeps
  34.  * track of how many times each word occurs
  35.  * then prints a report one word per line
  36.  * Amy Weintraub 2/1/96 - Data Structures homework 2 */
  37.  
  38. #include    <stdio.h>
  39. #include    <stdlib.h>
  40. #include    <string.h>
  41. #include    "wclist.h"
  42.  
  43. int main(int argc, char *argv[])
  44. {
  45.     char *currword = NULL;    /* pointer to current word */
  46.     struct node *head = NULL;    /* set empty list to point to NULL */
  47.     /* get a word and put it in a node */
  48.     while((currword = getword()) != NULL)  {
  49.         findlink(&head, currword);
  50.     }
  51.  
  52.     /* print list - should print words with number of occurrences */
  53.     printlist(head);
  54.  
  55.     return 0;
  56. }
  57. /* wc.c -- gets a word from stdin put it in a buffer and return
  58.  * NULL when done 
  59.  * getword()
  60.  * Amy Weintraub DS hw3 */
  61.  
  62. #include    <stdio.h>
  63. #include    <stdlib.h>
  64. #include    "wclist.h"
  65.  
  66. #define        TRUE     1
  67. #define        FALSE    0
  68. char * getword()
  69. {
  70.     char c;
  71.     char wordbuf[MAXWORD];
  72.     int wordflag = FALSE;    /* TRUE when in a word */
  73.     int i = 0;    /* index for wordbuf */
  74.     
  75.     while((c = getchar()) != EOF)  {
  76.         if(c != '\n' && c != ' ' && c != '\t' && wordflag == FALSE){
  77.             wordflag = TRUE;    /* start new word */
  78.             /* maybe add char checking for punctuation */
  79.             *(wordbuf + i) = c;    /* fill wordbuf with chars */
  80.             i++;
  81.         }    
  82.  
  83.         else if(c != '\n' && c != ' ' && c != '\t' && 
  84.                 wordflag == TRUE){
  85.             *(wordbuf + i) =c;
  86.             i++;
  87.         }
  88.  
  89.         if((c == ' ' || c == '\n' || c == '\t') &&
  90.                 wordflag == TRUE)  {
  91.             
  92.             wordflag = FALSE;    /* end of word */
  93.             *(wordbuf + i) = '\0'; /* null term array */
  94.  
  95.             /* return to main() to link it in */
  96.             return wordbuf;    /* returns local var */    
  97.         }
  98.     }
  99.     return NULL;     /* to show EOF reached by getword from stdin */        
  100. }
  101.  
  102. /* wclinked.c -- functions for linked list
  103.  * printlist(), errorfunc(), findlink(),
  104.  * Amy Weintraub Data Structures */
  105.  
  106. #include    <stdio.h>
  107. #include    <stdlib.h>
  108. #include    <string.h>
  109. #include    "wclist.h"
  110. /* printlist: visit each node in list and print it out */
  111. void printlist(struct node *head)
  112. {
  113.     struct node *pnode = head;
  114.  
  115.     while(pnode)  {
  116.         printf("%6d ", pnode->count);
  117.         printf("%s\n", pnode->data);
  118.         pnode = pnode->next;
  119.     }
  120. }
  121. /*************************************************************/
  122. /* error function:  prints message and exits program */
  123. void errorfunc(char *s)
  124. {
  125.     fprintf(stderr, "%s", s);
  126.     exit(1);
  127. }
  128. /*************************************************************/ 
  129. void findlink(struct node **head, char *word)  
  130. {
  131.     struct node *prev = *head;
  132.     struct node *curr = *head;
  133.     struct node *n;
  134.     int cmpval = -1;
  135.  
  136.     while(curr && (cmpval = strcmp(curr->data, word)) < 0) {
  137.         prev = curr;
  138.         curr = curr->next;
  139.     }
  140.  
  141.     /* if a match is found */
  142.     if(cmpval == 0)
  143.         curr->count++;
  144.  
  145.     /* no node exists so make one */
  146.     else  { 
  147.         if((n = (struct node *)malloc(sizeof(*n))) == NULL) 
  148.             errorfunc("Exhauted Memory");
  149.         n->count = 1;
  150.         n->data = strdup(word);
  151.         /* beginning? */
  152.         if((*head == NULL) || (*head == curr))  {
  153.             n->next =curr;
  154.             *head = n;
  155.         }
  156.  
  157.         /* insertion in middle or end */
  158.         else  {
  159.             n->next = curr;
  160.             prev->next = n;
  161.         }
  162.     }
  163. }    
  164.  
  165. /* wclist.h -- header for ll ds homework 2
  166.  * Amy Weintraub */
  167.  
  168. #define    MAXWORD        254    /* max line length in C */
  169. struct node {
  170.     char *data;        
  171.     int count;            /* number of times word occurs */
  172.     struct node *next;
  173. };
  174. extern void errorfunc(char *s);
  175. extern void printlist(struct node *head);
  176. extern void findlink(struct node **head, char *word);
  177. extern char * getword();
  178.  
  179.  
  180.  
  181.  
  182.  
  183.